local cp = {}

local function cardSort(a,b)
	return tonumber(a) < tonumber(b)
end

--检测是否可以碰
function cp.pengCheck(cardList,card)
  local count = 1
  for i,v in ipairs(cardList) do
    if card == v then
      if count == 3 then
        return true
      end
      count = count +1
    end
  end
  return false
end

--检测是否可以胡
function cp.huCheck(cardList)
  local removeList = {}
  local group = {gang={},kz={},sz={},dz={},wq=0}
  cp.checkGANG(cardList,1,#cardList,removeList,group)
  cp.removeCard(cardList,removeList,4)

  removeList = {}
  cp.checkKZ(cardList,1,#cardList,removeList,group)
  cp.removeCard(cardList,removeList,3)
  
  allDz = {}
  cp.getDZ(cardList,1,#cardList,allDz)
  
  --处理cardlist，分成两个数组
  local cardOne = {}
  local cardTwo = {}
  local count= {}
  for i,v in ipairs (cardList) do
    if not count[v] then
      table.insert(cardOne,v)
      count[v] = true
    else
      table.insert(cardTwo,v)
    end
  end
  removeList = {}
  cp.checkSZ(cardOne,1,#cardOne,removeList,group,allDz)
  cp.removeCard(cardOne,removeList,3)
  if #cardTwo > 0 then
    removeList = {}
    cp.checkSZ(cardTwo,1,#cardTwo,removeList,group)
    cp.removeCard(cardTwo,removeList,3)
  end
  cardList={}
  for i,v in ipairs (cardOne) do
      table.insert(cardList,v)
  end
   for i,v in ipairs (cardTwo) do
      table.insert(cardList,v)
  end
  table.sort(cardList,cardSort)

  cp.handleWq(group)

  removeList = {}
  cp.checkDZ(cardList,1,#cardList,removeList,group)
  cp.removeCard(cardList,removeList,2)
  
  if #cardList == 0 and #group.dz==1 then
    return true,group
  else
    return false,group
  end
end

--检测顺次
function cp.checkABC(value1,value2,value3)
  if (value1==value2-1) and (value2==value3-1) then return true
  else return false
    end
end

--检测对子
function cp.checkAA(value1,value2)
  if value1 == value2 then return true
else return false
  end
end

--检测三连对
function cp.checkAAA(value1,value2,value3)
  if cp.checkAA(value1,value2) and cp.checkAA(value2,value3) then return true
  else return false
    end
end

--检测四连对
function cp.checkAAAA(value1,value2,value3,value4)
  if cp.checkAA(value1,value2) and cp.checkAA(value2,value3) and cp.checkAA(value3,value4) then return true
  else return false
    end
end

function cp.existDZ(cardList,card)
  for i,v in ipairs(cardList) do
    if v ==card then
      return true
    end
  end
  return false
end

--整合顺次
function cp.checkSZ(card,i,n,removelist,group,allDz)
   if i>n then
    return
  end
  if n-i<2 then
    return
  end
  
  if allDz and cp.existDZ(allDz,card[i]) and not cp.existDZ(allDz,card[i+1])then 
      cp.checkSZ(card,i+1,n,removelist,group,allDz)
      return
  end
  
  if cp.checkABC(card[i],card[i+1],card[i+2]) then
     table.insert(removelist,i)
     table.insert(group.sz,card[i])
     cp.checkSZ(card,i+3,n,removelist,group,allDz)
  else
    cp.checkSZ(card,i+1,n,removelist,group,allDz)
  end
end

function cp.handleWq(group)
  local count = 0
  for i,v in ipairs(group.sz) do
     if v ==1 then
        count = count+1
        table.remove(group.sz,i)
     end
  end
  group.wq = count
end

--整合杠
function cp.checkGANG(card,i,n,removelist,group)
  if i>n then
    return
  end
  if n-i<3 then
    return
  end

  if cp.checkAAAA(card[i],card[i+1],card[i+2],card[i+3]) then
     table.insert(removelist,i)
     table.insert(group.gang,card[i])
     cp.checkGANG(card,i+4,n,removelist,group)
  else
    cp.checkGANG(card,i+1,n,removelist,group)
  end
end

--整合三连对
function cp.checkKZ(card,i,n,removelist,group)
  if i>n then
    return
  end
  if n-i<2 then
    return
  end
  if cp.checkAAA(card[i],card[i+1],card[i+2]) then
     table.insert(removelist,i)
     table.insert(group.kz,card[i])
     cp.checkKZ(card,i+3,n,removelist,group)
  else
      cp.checkKZ(card,i+1,n,removelist,group)
    end
end

--整合对子
function cp.checkDZ(card,i,n,removelist,group)
    if i>n then
    return
  end
  if n-i<1 then
    return
  end
   if cp.checkAA(card[i],card[i+1]) then
     table.insert(removelist,i)
     table.insert(group.dz,card[i])
     cp.checkDZ(card,i+2,n,removelist,group)
  else
     cp.checkDZ(card,i+1,n,removelist,group)
    end
end

function cp.getDZ(card,i,n,dzGroup)
    if i>n then
    return
  end
  if n-i<1 then
    return
  end
   if cp.checkAA(card[i],card[i+1]) then
     table.insert(dzGroup,card[i])
     cp.getDZ(card,i+2,n,dzGroup)
  else
     cp.getDZ(card,i+1,n,dzGroup)
    end
end

function cp.removeCard(card,removeList,n)
  local count = 0
  for i,v in ipairs(removeList) do
    v = v-count
    for i=0,n-1 do
        table.remove(card,v)
    end
    count = count + n
  end
end

return cp